package com.chenggcode.simple.jpa;

import com.chenggcode.simple.jpa.entity.User;
import com.chenggcode.simple.jpa.repository.UserRepository;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Chenggcode
 * @date 2025/7/1 9:52
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final UserRepository userRepository;

    @Operation(summary = "创建用户")
    @PostMapping
    public User create(@RequestBody User user) {
        return userRepository.save(user);
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/getAll")
    public List<User> getAll() {
        return userRepository.findAll();
    }


    @Operation(summary = "根据用户名查询")
    @GetMapping("/getByUsername")
    public List<User> getByUsername(@RequestParam("username") String username) {
        return userRepository.findByUsernameOrderByCreatedAtDesc(username);
    }

    @Operation(summary = "根据用户名模糊查询")
    @GetMapping("/getByUsernameContaining")
    public List<User> getByUsernameContaining(@RequestParam("username") String username) {
        return userRepository.findByUsernameContaining(username);
    }

}
